<!--
 * @Descripttion: 元数据维护
 * @version: 
 * @Author: Civet
 * @Date: 2019-09-29 09:25:20
 -->
<template>
  <MaintenanceView
    :treeData="treeData"
    :treeProps="treeProps"
    :componentType="componentType"
    :showtableData="showtableData"
    :showTableList="showTableList"
    :systemForm="systemForm"
    :sysForm="sysForm"
    :sysDialog="sysDialog"
    :sysfindAllSysData="sysfindAllSysData"
    :findOrgCatalogTreeData="findOrgCatalogTreeData"
    :QueryDictInfoByTypeData="QueryDictInfoByTypeData"
    :sysexchangeSystemData="sysexchangeSystemData"
    :queryLabelTree="queryLabelTree"
    :infoCollectData="infoCollectData"
    :infotableData="infotableData"
    :editInfoForm="editInfoForm"
    :baseInfoDialog="baseInfoDialog"
    :ExtractionMethodData="ExtractionMethodData"
    :DatabaseTypeData="DatabaseTypeData"
    :dbVersion="dbVersion"
    :driveData="driveData"
    :exchangeSystemData="exchangeSystemData"
    :findAllSysData="findAllSysData"
    :infoOptions="infoOptions"
    @loadNode="loadNode"
    @nodeClick="nodeClick"
    @sysEdit="sysEdit"
    @sysClose="sysClose"
    @sysCancel="sysCancel"
    @sysKeep="sysKeep"
    @baseInfoEdit="baseInfoEdit"
    @baseInfoClose="baseInfoClose"
    @baseInfoCancel="baseInfoCancel"
    @baseInfoKeep="baseInfoKeep"
    @baseSearchDbVersion="baseSearchDbVersion"
    @baseSearchDbDrive="baseSearchDbDrive"
  ></MaintenanceView>
</template>
<script>
import MaintenanceView from "./maintenanceView";
import metadatamaintenanceApi from "@/httpUtils/ExternalManagement/maintenance";
import metadataRegistrationApi from "@/httpUtils/ExternalManagement/metadataRegistration";

export default {
  name: "maintenanceLogic",
  components: {
    MaintenanceView
  },
  watch: {},
  props: {},
  filters: {},
  data() {
    return {
      treeData: [], // 资源档案目录树
      treeProps: {
        children: "child",
        label: "name"
      },
      componentType: "", // 组件名称
      showtableData: [], // 表格数据
      showTableList: [], // 表格头
      sysDic: [
        { id: "index", name: "序号" },
        { id: "system_name", name: "名称" },
        { id: "system_summary", name: "系统概述" },
        { id: "submit_time", name: "汇交时间" },
        { id: "user_id", name: "注册人" }
      ],
      sqlDic: [
        { id: "db_url", name: "数据访问URL" },
        { id: "dict_name", name: "数据库类型" },
        { id: "user_name", name: "注册人" }
      ],
      fileDic: [
        { id: "access_address", name: "文件访问URL" },
        { id: "xxx", name: "注册人" }
      ],
      interfaceDic: [
        { id: "access_address", name: "接口访问URL" },
        { id: "xxx", name: "注册人" }
      ],
      tableDic: [
        { id: "index", name: "序号" },
        { id: "table_name", name: "表名" },
        { id: "registerTime", name: "数据现势性" },
        { id: "user_name", name: "注册人" }
      ],
      outSystem: "", // 用来装系统id
      outSql: "", // 用来装sqlid

      // ---------------------------SystemForm
      systemForm: {},
      sysForm: {
        system_name: "",
        parent_system: "",
        build_department: null,
        submit_time: "",
        use_department: [],
        submit_way: "",
        system_contractor: "",
        system_security_level: "",
        system_version: "",
        label: [],
        system_summary: "",
        remark: ""
      },
      sysDialog: false,
      sysfindAllSysData: [],
      findOrgCatalogTreeData: [],
      QueryDictInfoByTypeData: [],
      sysexchangeSystemData: [],
      queryLabelTree: [],
      // -----------------------------InfoCollect

      infotableData: [],
      infoCollectData: {},
      baseInfoDialog: false,
      editInfoForm: {
        name: "",
        dataExtractionMethod: "",
        dbType: "",
        sourcePath: "",
        dbVersion: "",
        strogePath: "",
        dbDrive: "",
        secretLevel: "",
        dbUrl: "",
        systemId: "",
        username: "",
        description: "",
        dbPwd: "",
        label: ""
      },
      ExtractionMethodData: [],
      DatabaseTypeData: [],
      dbVersion: [],
      driveData: [],
      exchangeSystemData: [],
      findAllSysData: [],
      infoOptions: []
    };
  },
  created() {},
  mounted() {},
  methods: {
    /**
     * @description: 懒加载
     * @param {object} node data 树节点信息
     * @param {function} resolve 通过此方法将请求到的数据放入
     * @return:
     * @author: Civet
     */
    loadNode(node, resolve) {
      // console.log("node", node);
      // console.log("node.data", node.data);
      // console.log("node.level", node.level);
      if (node.level === 0) {
        metadatamaintenanceApi.findResourceCatalog().then(res => {
          if (res.data.code === 200) {
            return resolve(res.data.data);
          } else {
            this.$message.error("资源档案目录获取失败");
          }
        });
      }

      if (node.level !== 0) {
        if (node.data.child.length > 0) {
          return resolve(node.data.child);
        }

        if (node.data.name === "业务系统资源") {
          metadatamaintenanceApi.findAllSysForMaintain().then(res => {
            if (res.data.code === 200) {
              return resolve(res.data.data);
            } else {
              this.$message.error("获取系统列表失败");
            }
          });
        } else if (node.data.name === "数据库") {
          metadatamaintenanceApi
            .findDbBySystemId({ systemId: node.parent.data.system_id })
            .then(res => {
              if (res.data.code === 200) {
                return resolve(res.data.data);
              } else {
                this.$message.error("获取数据库列表失败");
              }
            });
        } else {
          resolve([]);
        }
      }
    },
    /**
     * @description: 树节点点击
     * @param {Object} data 树节点信息
     * @return:
     * @author: Civet
     */
    nodeClick(data) {
      console.log("sdsd", data);
      switch (data.type) {
        case "catalog":
          this.componentType = "ShowTable";
          this.showtableData = [];
          this.showTableList = [];

          if (data.name === "业务系统资源") {
            this.showTableList = this.sysDic;
            metadatamaintenanceApi.findAllSysForMaintain().then(res => {
              if (res.data.code === 200) {
                this.showtableData = res.data.data;
              } else {
                this.$message.error("获取系统列表失败");
              }
            });
          } else if (data.name === "数据库") {
            this.showTableList = this.sqlDic;
            metadatamaintenanceApi
              .findDbBySystemId({ systemId: data.systemId })
              .then(res => {
                if (res.data.code === 200) {
                  this.showtableData = res.data.data;
                } else {
                  this.$message.error("获取数据库列表失败");
                }
              });
          } else if (data.name === "文件") {
            this.showTableList = this.fileDic;
            metadatamaintenanceApi
              .findAllFileBySystemId({ sysId: data.systemId })
              .then(res => {
                if (res.data.code === 200) {
                  this.showtableData = res.data.data;
                } else {
                  this.$message.error("获取文件列表失败");
                }
              });
          } else if (data.name === "接口") {
            this.showTableList = this.interfaceDic;
            metadatamaintenanceApi
              .findAllInterfaceBySystemId({ sysId: data.systemId })
              .then(res => {
                if (res.data.code === 200) {
                  this.showtableData = res.data.data;
                } else {
                  this.$message.error("获取接口列表失败");
                }
              });
          } else if (
            data.name === "传统业务表" ||
            data.name === "空间数据表" ||
            data.name === "空间变化索引表"
          ) {
            this.showTableList = this.tableDic;
            metadatamaintenanceApi
              .findTabBytableTypeIdForMaintain({
                tableTypeId: data.tableTypeId,
                dbId: data.dbId
              })
              .then(res => {
                if (res.data.code === 200) {
                  this.showtableData = res.data.data;
                } else {
                  this.$message.error("获取数据库列表失败");
                }
              });
          }
          break;
        case "system":
          this.componentType = "SystemForm";
          this.outSystem = data.system_id;
          this.findSys();
          break;
        case "sql":
          // this.infoCollectData = data;
          this.outSql = data.db_id;
          this.infotableData = data.child;
          this.componentType = "InforCollect";
          this.findSql();
          break;
        case "table":
          this.componentType = "SpaceForm";
          break;
      }
      console.log("this.componentType", this.componentType);
    },
    // -------------------------------- SystemForm方法
    // 根据系统id获取系统信息
    findSys() {
      metadatamaintenanceApi.findSys({ sysId: this.outSystem }).then(res => {
        this.systemForm = res.data.data[0];
      });
    },
    findSql() {
      metadatamaintenanceApi.findSql({ dbId: this.outSql }).then(res => {
        this.infoCollectData = res.data.data[0];
      });
    },
    // 编辑系统
    sysEdit() {
      // //  查询到所有的父级系统
      // metadataRegistrationApi.findAllSys().then(res => {
      //   this.sysfindAllSysData = res.data.data;
      // });

      //  建设部门，使用部门
      metadataRegistrationApi.findOrgCatalogTree().then(res => {
        this.IterationDelateMenuChildren(res.data.data);
        this.findOrgCatalogTreeData = res.data.data;
      });

      //  汇交方式
      metadataRegistrationApi
        .getQueryDictInfoByType({ typeCode: "1000001320520190" })
        .then(res => {
          this.QueryDictInfoByTypeData = res.data.data;
        });

      //  系统密集
      metadataRegistrationApi
        .exchangeSystem({ typeCode: "1000001020219608" })
        .then(res => {
          this.sysexchangeSystemData = res.data.data;
        });

      //  标签管理
      metadataRegistrationApi.queryLabelTree().then(res => {
        this.IterationDelateMenuChildren(res.data.data);
        this.queryLabelTree = res.data.data;
      });

      let systemForm = this.systemForm;
      console.log("systemForm", systemForm);
      this.sysForm = {
        system_name: systemForm.system_name,
        parent_system: systemForm.parent_system,
        build_department: systemForm.build_department,
        submit_time: systemForm.submit_time,
        use_department: systemForm.use_department.split(","),
        submit_way: systemForm.submit_way,
        system_contractor: systemForm.system_contractor,
        system_security_level: systemForm.system_security_level,
        system_version: systemForm.system_version,
        label: systemForm.label == "" ? null : systemForm.label.split(","),
        system_summary: systemForm.system_summary,
        remark: systemForm.remark
      };

      // 获取父级系统
      metadatamaintenanceApi
        .updateFindOfSystem({ sysId: systemForm.system_id })
        .then(res => {
          console.log("asadad", res);
          this.sysfindAllSysData = res.data.data;
        });

      this.sysDialog = true;
    },
    // 关闭编辑弹窗接口
    sysClose() {
      this.sysDialog = false;
    },
    // 取消编辑弹窗接口
    sysCancel() {
      this.sysDialog = false;
    },
    // 确定弹窗接口
    sysKeep() {
      let sysForm = this.sysForm;
      console.log("sysForm", sysForm);
      let obj = {
        buildDepartment: sysForm.build_department,
        label: sysForm.label ? sysForm.label.join(",") : "",
        parentSystem: sysForm.parent_system,
        remark: sysForm.remark,
        submitTime: sysForm.submit_time,
        submitWay: sysForm.submit_way,
        systemContractor: sysForm.system_contractor,
        systemId: this.outSystem,
        systemName: sysForm.system_name,
        systemSecurityLevel: sysForm.system_security_level,
        systemSummary: sysForm.system_summary,
        systemVersion: sysForm.system_version,
        useDepartment: sysForm.use_department
          ? sysForm.use_department.join(",")
          : ""
      };

      metadatamaintenanceApi.UpdateSystem(obj).then(res => {
        if (res.data.code === 200) {
          this.findSys();
          this.$message.success("修改成功");
        } else {
          this.$message.error("修改失败");
        }
      });

      this.sysDialog = false;
    },
    // --------------------------------- InfoCollect
    // 编辑
    baseInfoEdit() {
      //  数据抽取方式
      metadataRegistrationApi
        .exchangeSystem({ typeCode: "1000000046892521" })
        .then(res => {
          this.ExtractionMethodData = res.data.data;
        });
      // //  数据储存类型
      // metadataRegistrationApi
      //   .DataStorageStrategy({ typeCode: "1000000301862824" })
      //   .then(res => {
      //     console.log("数据库类型", res);
      //     this.DataStorageStrategyData = res.data.data;
      //   });

      // 数据库类型
      metadataRegistrationApi
        .exchangeSystem({ typeCode: "1000000800072811" })
        .then(res => {
          this.DatabaseTypeData = res.data.data;
        });
      // 数据版本
      metadataRegistrationApi
        .queryInfoByDictId({ dictId: this.infoCollectData.db_type })
        .then(res => {
          this.dbVersion = res.data.data;
        });
      //  驱动
      metadataRegistrationApi
        .queryInfoByDictId({ dictId: this.infoCollectData.db_version })
        .then(res => {
          this.driveData = res.data.data;
        });
      //  系统密集
      metadataRegistrationApi
        .exchangeSystem({ typeCode: "1000001020219608" })
        .then(res => {
          this.exchangeSystemData = res.data.data;
        });

      //  查询到所有的父级系统
      metadataRegistrationApi.findAllSys().then(res => {
        this.findAllSysData = res.data.data;
      });

      //  标签管理
      metadataRegistrationApi.queryLabelTree().then(res => {
        this.IterationDelateMenuChildren(res.data.data);
        this.infoOptions = res.data.data;
      });
      let infoCollectData = this.infoCollectData;
      console.log("infoCollectData", infoCollectData);
      this.editInfoForm = {
        name: infoCollectData.db_name,
        dataExtractionMethod: infoCollectData.data_extraction_method,
        dbType: infoCollectData.db_type,
        sourcePath: infoCollectData.source_path,
        dbVersion: infoCollectData.db_version,
        strogePath: infoCollectData.storage_path,
        dbDrive: infoCollectData.db_drive,
        secretLevel: infoCollectData.secret_level,
        dbUrl: infoCollectData.db_url,
        systemId: infoCollectData.system_id,
        username: infoCollectData.user_name,
        description: infoCollectData.description,
        dbPwd: infoCollectData.db_pwd,
        label:
          infoCollectData.label == "" ? null : infoCollectData.label.split(",")
      };

      this.baseInfoDialog = true;
    },
    // 关闭弹窗
    baseInfoClose() {
      this.baseInfoDialog = false;
    },
    // 取消弹窗
    baseInfoCancel() {
      this.baseInfoDialog = false;
    },
    // 保存弹窗
    baseInfoKeep() {
      let obj = {
        dataExtractionMethod: this.editInfoForm.dataExtractionMethod,
        dbDrive: this.editInfoForm.dbDrive,
        dbId: this.infoCollectData.db_id,
        dbName: this.editInfoForm.name,
        dbPwd: this.editInfoForm.dbPwd,
        dbType: this.editInfoForm.dbType,
        dbUrl: this.editInfoForm.dbUrl,
        dbVersion: this.editInfoForm.dbVersion,
        description: this.editInfoForm.description,
        label: this.editInfoForm.label ? this.editInfoForm.label.join(",") : "",
        secretLevel: this.editInfoForm.secretLevel,
        sourcePath: this.editInfoForm.sourcePath,
        storagePath: this.editInfoForm.strogePath,
        systemId: this.editInfoForm.systemId,
        userName: this.editInfoForm.username
      };

      metadatamaintenanceApi.UpdateSql(obj).then(res => {
        if (res.data.code === 200) {
          this.findSql();
          this.$message.success("修改成功");
        } else {
          this.$message.error("修改失败");
        }
      });

      this.baseInfoDialog = false;
    },
    /**
     * @description: 数据库类型联动数据库版本
     * @param {string} val 数据库类型id
     * @return: 
     * @author: Civet
     */
    baseSearchDbVersion(val) {
      this.editInfoForm.dbVersion = "";
      this.editInfoForm.dbDrive = "";
      this.driveData = [];
      metadataRegistrationApi.queryInfoByDictId({ dictId: val }).then(res => {
        this.dbVersion = res.data.data;
      });
    },
    /**
     * @description: 数据库版本联动数据库驱动
     * @param {string} val 数据库版本id
     * @return: 
     * @author: Civet
     */
    baseSearchDbDrive(val) {
      this.editInfoForm.dbDrive = "";
      metadataRegistrationApi.queryInfoByDictId({ dictId: val }).then(res => {
        this.driveData = res.data.data;
      });
    },
    // ----------------------- SystemForm 重置
    resetSysForm() {
      this.sysForm = {
        system_name: "",
        parent_system: "",
        build_department: "",
        submit_time: "",
        use_department: "",
        submit_way: "",
        system_contractor: "",
        system_security_level: "",
        system_version: "",
        label: "",
        system_summary: "",
        remark: ""
      };
    }
  },
  destroyed() {}
};
</script>
